<template>
  <div class="app-container">
    <el-form
      :model="addFrom"
      :rules="rules"
      ref="addFrom"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="轮播图名称" prop="carouselName">
        <el-input v-model="addFrom.carouselName"></el-input>
      </el-form-item>
      <el-form-item label="轮播图重定向地址" prop="carouselRedirectUrl">
        <el-input v-model="addFrom.carouselRedirectUrl"></el-input>
      </el-form-item>
      <el-form-item label="轮播图排序值" prop="carouselRank">
        <el-input v-model="addFrom.carouselRank"></el-input>
      </el-form-item>
      <el-form-item label="上传文件">
        <el-upload
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList"
          list-type="picture"
          with-credentials
          :multiple="false"
          :auto-upload="false"
          before-upload:beforeupload
          :on-change="setFile"
          :limit="1"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('addFrom')">添加轮播图</el-button>
        <el-button @click="resetForm('addFrom')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addCarousel } from "@/api/carousel";
export default {
  data() {
    return {

      rules: {
        carouselName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        carouselRedirectUrl: [
          { required: true, message: "地址不能为空", trigger: "blur" },
        ],
        carouselRank: [
          { required: true, message: "排序值不能空", trigger: "blur" },
        ],
      },

      addFrom: {
        carouselName: "",
        carouselRedirectUrl: "",
        carouselRank: "",
      },

      // 文件列表
      fileList: [],

      file: "",
    };
  },

  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },

    beforeupload(file) {
      console.log(file);
    },

    setFile(file, fileList) {
      this.file = file;
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var data = new FormData();

          data.append("carouselName", this.addFrom.carouselName);
          data.append("carouselRedirectUrl", this.addFrom.carouselRedirectUrl);
          data.append("carouselRank", this.addFrom.carouselRank);
          // 文件列表
          console.log(this.file.raw);
          data.append("file", this.file.raw);
          addCarousel(data).then((res) => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },

  created() {},
};
</script>

<style>
</style>